using Bag;
using Config;
using Data;
using Entity;
using GameTime;
using MoonFramework.Scripts.Mono;
using MoonFramework.Scripts.Tool.System.Setting;
using Tool;
using Tool.Name;
using UnityEngine;

namespace Map.MapObject.Building
{
    public class Campfire : BaseBuilding
    {
        [SerializeField] private Light _light;
        [SerializeField] private GameObject _fire;
        [SerializeField] private AudioSource _audioSource;
        private CampfireConfig _campfireConfig;
        private CampfireData _campfireData;
        private bool _isInit;

        public override void Init(BaseMapBlock mapBlock, ulong id)
        {
            base.Init(mapBlock, id);
            _campfireConfig = ConfigManager.Instance.GetConfig<CampfireConfig>(ConfigName.Campfire, 0);
            //TODO:持久化数据 存档
            _campfireData = new CampfireData
            {
                CurFuel = _campfireConfig.DefaultFuelValue
            };
            ArchiveManager.Instance.AddIMapObjectTypeData(id, _campfireData);
            SetLight(_campfireData.CurFuel);
            MonoManager.Instance.SendUpdateEvent(OnUpdate);
            _isInit = true;
        }

        /// <summary>
        /// 预览时
        /// </summary>
        public override void OnPreview()
        {
            //关闭粒子和火焰效果
            SetLight(0);
        }

        private void OnUpdate()
        {
            if(_campfireData.CurFuel == 0) return;
            //减少燃料
            _campfireData.CurFuel = Mathf.Clamp(_campfireData.CurFuel - _campfireConfig.FuelConsumptionSpeed * TimeManager.Instance.timeScale* Time.deltaTime, 0, _campfireConfig.FuelMaxValue);
            SetLight(_campfireData.CurFuel);
        }
        
        /// <summary>
        /// 设置燃料
        /// </summary>
        private void SetLight(float fuelValue)
        {
            _light.gameObject.SetActive( fuelValue != 0);
            _fire.gameObject.SetActive( fuelValue != 0);
            _audioSource.gameObject.SetActive(fuelValue != 0);
            if (fuelValue != 0)
            {
                float value = fuelValue / _campfireConfig.FuelMaxValue;
                //设置亮度
                _light.intensity = Mathf.Lerp(0, _campfireConfig.MaxLightIntensity, value);
                //设置范围
                _light.range = Mathf.Lerp(0, _campfireConfig.MaxLightRange, value);
                //设置篝火音量
                _audioSource.volume = value;
            }
        }

        public override bool OnSlotEndDragSelect(int itemID)
        {
            //木材，燃料等作为燃料物品
            if (_campfireConfig.TryGetFuelValueByItemID(itemID, out float fuelValue))
            {
                _campfireData.CurFuel = Mathf.Clamp(_campfireData.CurFuel + fuelValue, 0, _campfireConfig.FuelMaxValue);
                SetLight(_campfireData.CurFuel);
                return true;
            }
            //烘焙检查与烧烤检查
            if (_campfireConfig.TryGetBakedItemByItemID(itemID, out int bakedID))
            {
                //有没有燃料
                if (_campfireData.CurFuel <= 0) return false;
                //给玩家添加一个物品
                return BagManager.Instance.AddItemAndCheck(bakedID);
            }
            return false;
        }

        public override bool DoHit(Actor actor, float dir)
        {
            return false;
        }

        public override void Hurt(ItemWeaponInfo weapon) { }

        protected override void Dead() { }
        
        public override void OnSelect() { }

        public override bool CollisionDetectionOnPreview(LayerMask layerMask)
        {
            return CapsuleCollisionDetectionOnPreview(layerMask);
        }

        private void OnEnable()
        {
            if(_isInit)
                MonoManager.Instance.SendUpdateEvent(OnUpdate);
        }

        private void OnDisable()
        {
            MonoManager.Instance.RemoveUpdateEvent(OnUpdate);
        }
    }
}